﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

// qt headers
#include <QWidget>

// gcmp headers

// custom headers
#include "PermissionWatcher.h"
#include "Permission.h"
#include "IGmepGcsProjectManager.h"
#include "GbmpNew.h"
#include "DebugMessage.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    PermissionWatcher::PermissionWatcher(QObject *parent/* = nullptr*/) :
        m_currentPermission(Permission::UNKNOWN),
        m_vWatchedObjectBehaviors()
    {
        Init();
    }

    PermissionWatcher::~PermissionWatcher()
    {

    }

    PermissionWatcher * PermissionWatcher::s_pInstance = nullptr;
    PermissionWatcher * PermissionWatcher::Get()
    {
        if (!PermissionWatcher::s_pInstance)
        {
            PermissionWatcher::s_pInstance = NEW_AS_QT(PermissionWatcher);
        }
        
        return PermissionWatcher::s_pInstance;
    }

    void PermissionWatcher::CheckPermission()
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(IProjectManager::Get(), L"IProjectManager::Get()为空", L"GMEP", L"2024-01-30");
        auto currentPermission = IProjectManager::Get()->GetCurrentProjectPermission();
        if (m_currentPermission != currentPermission)
        {
            auto oldPermission = m_currentPermission;
            m_currentPermission = currentPermission;
            emit CurrentPermissionChanged(oldPermission, m_currentPermission);
        }
    }

    void PermissionWatcher::Watch(QObject * target, void(*behavior)(QObject *targetObject, const int currentPermission))
    {
        m_vWatchedObjectBehaviors.push_back(std::pair<QObject*, void(*)(QObject* targetObject, const int currentPermission)>(target, behavior));
    }

    void PermissionWatcher::Watch(QWidget * target, void(*behavior)(QWidget *targetWidget, const int currentPermission))
    {
        m_vWatchedWidgetBehaviors.push_back(std::pair<QWidget*, void(*)(QWidget* targetWidget, const int currentPermission)>(target, behavior));
    }

    int PermissionWatcher::GetCurrentPermission()
    {
        return m_currentPermission;
    }

    void PermissionWatcher::TestSwitchPermission()
    {
        auto currentPermission = m_currentPermission == Permission::SOFTWRITE ? Permission::UNKNOWN : Permission::SOFTWRITE;
        if (m_currentPermission != currentPermission)
        {
            auto oldPermission = m_currentPermission;
            m_currentPermission = currentPermission;
            emit CurrentPermissionChanged(oldPermission, m_currentPermission);
        }
    }

    void PermissionWatcher::Init()
    {
        connect(this, &PermissionWatcher::CurrentPermissionChanged, [=](const int oldPermission, const int newPermission) {
            for (auto &watched : m_vWatchedObjectBehaviors)
            {
                if (!watched.first)
                {
                    continue;
                }
                watched.second(watched.first, newPermission);
            }

            for (auto &watched : m_vWatchedWidgetBehaviors)
            {
                if (!watched.first)
                {
                    continue;
                }
                watched.second(watched.first, newPermission);
            }
        });
    }

}